<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="菜单列表"
      row-key="id"
      :indent="20"
      :columns="columns"
      :request-api="getSystemResourceTree"
      :pagination="false"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'system_resource_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">
          新增权限节点
        </el-button>
      </template>
      <!-- 菜单操作 -->
      <template #operation="scope">
        <el-button v-auth="'system_resource_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'system_resource_del'" type="danger" link :icon="Delete" @click="deleteSystemResource(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <MenuEdit ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="menuMange">
import { ref } from "vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, CirclePlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { getSystemResourceTree } from "@/api/modules/system";
import { yesOrNoStatus } from "@/utils/dict";
import MenuEdit from "@/views/system/menuMange/MenuEdit.vue";
import { System } from "@/api/interface";
import { saveSystemResource, delSystemResource } from "@/api/modules/system";
import { useHandleData } from "@/hooks/useHandleData";

const proTable = ref();
// 表格配置项
const columns: ColumnProps[] = [
  { prop: "name", label: "菜单" },
  { prop: "rightKey", label: "菜单标识" },
  { prop: "isMenu", label: "是否菜单", tag: true, enum: yesOrNoStatus },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
];

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof MenuEdit> | null>(null);
const openDrawer = (title: string, row: Partial<System.SystemResourceTree> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveSystemResource,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};

// 删除权限资源
const deleteSystemResource = async (params: System.SystemResourceTree) => {
  await useHandleData(delSystemResource, { ids: params.id }, `删除【${params.name}】权限节点`);
  proTable.value?.getTableList();
};
</script>
